#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>

// Base64编码表
static const char base64_encoding_table[] = {
    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
    'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
    'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
    'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
    'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
    'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
    'w', 'x', 'y', 'z', '0', '1', '2', '3',
    '4', '5', '6', '7', '8', '9', '+', '/'
};

// Base64解码表
static const int base64_decoding_table[256] = {
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55,
    56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1,  0,  1,  2,
     3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,
    18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27,
    28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42,
    43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
};

// 计算Base64编码后的长度
size_t base64_encode_length(size_t src_length) {
    return (src_length + 2) / 3 * 4 + 1; // +1 for null terminator
}

// 计算Base64解码后的最大长度
size_t base64_decode_length(const char *src) {
    size_t len = strlen(src);
    size_t padding = 0;

    if (len >= 2) {
        if (src[len-1] == '=') padding++;
        if (src[len-2] == '=') padding++;
    }

    return (len * 3) / 4 - padding;
}

// Base64编码函数
char *base64_encode(const unsigned char *data, size_t src_length, size_t *output_length) {
    size_t encoded_length = base64_encode_length(src_length);
    char *encoded_data = (char *)malloc(encoded_length);
    if (encoded_data == NULL) return NULL;

    for (size_t i = 0, j = 0; i < src_length;) {
        uint32_t octet_a = i < src_length ? (unsigned char)data[i++] : 0;
        uint32_t octet_b = i < src_length ? (unsigned char)data[i++] : 0;
        uint32_t octet_c = i < src_length ? (unsigned char)data[i++] : 0;

        uint32_t triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c;

        encoded_data[j++] = base64_encoding_table[(triple >> 3 * 6) & 0x3F];
        encoded_data[j++] = base64_encoding_table[(triple >> 2 * 6) & 0x3F];
        encoded_data[j++] = base64_encoding_table[(triple >> 1 * 6) & 0x3F];
        encoded_data[j++] = base64_encoding_table[(triple >> 0 * 6) & 0x3F];
    }

    // 添加填充
    if (src_length > 0) {
        size_t padding = src_length % 3;
        if (padding > 0) {
            for (size_t i = 0; i < (3 - padding); i++) {
                encoded_data[base64_encode_length(src_length) - 2 - i] = '=';
            }
        }
    }

    encoded_data[encoded_length - 1] = '\0';
    if (output_length != NULL) *output_length = encoded_length - 1;

    return encoded_data;
}

// Base64解码函数
unsigned char *base64_decode(const char *data, size_t src_length, size_t *output_length) {
    if (src_length % 4 != 0) return NULL;

    size_t decoded_length = base64_decode_length(data);
    unsigned char *decoded_data = (unsigned char *)malloc(decoded_length);
    if (decoded_data == NULL) return NULL;

    for (size_t i = 0, j = 0; i < src_length;) {
        uint32_t sextet_a = data[i] == '=' ? 0 & i++ : base64_decoding_table[(int)data[i++]];
        uint32_t sextet_b = data[i] == '=' ? 0 & i++ : base64_decoding_table[(int)data[i++]];
        uint32_t sextet_c = data[i] == '=' ? 0 & i++ : base64_decoding_table[(int)data[i++]];
        uint32_t sextet_d = data[i] == '=' ? 0 & i++ : base64_decoding_table[(int)data[i++]];

        uint32_t triple = (sextet_a << 3 * 6)
                        + (sextet_b << 2 * 6)
                        + (sextet_c << 1 * 6)
                        + (sextet_d << 0 * 6);

        if (j < decoded_length) decoded_data[j++] = (triple >> 2 * 8) & 0xFF;
        if (j < decoded_length) decoded_data[j++] = (triple >> 1 * 8) & 0xFF;
        if (j < decoded_length) decoded_data[j++] = (triple >> 0 * 8) & 0xFF;
    }

    if (output_length != NULL) *output_length = decoded_length;

    return decoded_data;
}


